﻿using Avalonia.Data;
using Avalonia.Data.Converters;
using System;
using System.Globalization;

namespace Avalonia.BiliBili.Libs.Extensions.Converter
{
    public sealed class ImageUriConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (value is string sourceText)
                {
                    if (!sourceText.StartsWith("http"))
                        sourceText = $"https:{sourceText}";
                    if (sourceText.Contains("@"))
                        return new Uri(sourceText);
                    else
                    {
                        var paras = System.Convert.ToInt32(parameter) switch
                        {
                            0 => "@300w.jpg",
                            1 => "@500w.jpg",
                            _ => "@800w.jpg"
                        };
                        return new Uri(string.Concat(sourceText, paras));
                    }
                }
            }
            catch { }
            return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}